package com.industrial.app.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.industrial.app.domain.BizPowerConsumption;
import com.industrial.app.service.IBizPowerConsumptionService;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 重点省份与全国工业用电量同比Controller
 * 
 * @author Zhou_S
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/biz/consumption")
public class BizPowerConsumptionController extends BaseController
{
    @Autowired
    private IBizPowerConsumptionService bizPowerConsumptionService;

    /**
     * 查询重点省份与全国工业用电量同比列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPowerConsumption bizPowerConsumption)
    {
        if(StringUtils.isNotBlank(bizPowerConsumption.getDates())){
            DateTime datesTimes= DateUtil.parse(bizPowerConsumption.getDates(),"yyyy年MM月");
            bizPowerConsumption.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        }else{
            throw new RuntimeException("未指定日期！");
        }
        startPage();
        List<BizPowerConsumption> list = bizPowerConsumptionService.selectBizPowerConsumptionList(bizPowerConsumption);
        return getDataTable(list);
    }

    /**
     * 导出重点省份与全国工业用电量同比列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:export')")
//    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPowerConsumption bizPowerConsumption)
    {
        List<BizPowerConsumption> list = bizPowerConsumptionService.selectBizPowerConsumptionList(bizPowerConsumption);
        ExcelUtil<BizPowerConsumption> util = new ExcelUtil<BizPowerConsumption>(BizPowerConsumption.class);
        util.exportExcel(response, list, "重点省份与全国工业用电量同比数据");
    }

    /**
     * 获取重点省份与全国工业用电量同比详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizPowerConsumptionService.selectBizPowerConsumptionByBaseId(baseId));
    }

    /**
     * 新增重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:add')")
//    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPowerConsumption bizPowerConsumption)
    {
        return toAjax(bizPowerConsumptionService.insertBizPowerConsumption(bizPowerConsumption));
    }

    /**
     * 修改重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:edit')")
//    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPowerConsumption bizPowerConsumption)
    {
        return toAjax(bizPowerConsumptionService.updateBizPowerConsumption(bizPowerConsumption));
    }

    /**
     * 删除重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('biz:consumption:remove')")
//    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizPowerConsumptionService.deleteBizPowerConsumptionByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
//    @Anonymous
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizPowerConsumptionService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    /**
     * <p>
     *     清空所有的重点省份与全国工业用电量同比数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizPowerConsumptionService.clearAllTableData();
        return AjaxResult.success();
    }
    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
//    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizPowerConsumptionService.transferDataInOneClick());
    }

}
